package top.revery.task.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import top.revery.api.entity.user.BaseUserKol;
import top.revery.task.dto.KolDto;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import java.util.Map;

/**
 * @author denghao
 * @title 网红
 * @package
 * @date 2019/12/13 9:24
 */
@Service
public class KolService {

    @Autowired
    private KolDto kolDto;

    public Page<BaseUserKol> findPage(int pageNum, int pageSize, Map map) {
        Specification<BaseUserKol> specification = createSpecification(map);
        PageRequest pageRequest = PageRequest.of(pageNum - 1, pageSize);
        return kolDto.findAll(specification, pageRequest);

    }

    private Specification<BaseUserKol> createSpecification(Map map) {
        return new Specification<BaseUserKol>() {
            @Override
            public Predicate toPredicate(Root<BaseUserKol> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate predicate = cb.conjunction();
                //id
                if (map.get("id") != null && !"".equals(map.get("id"))) {
                    predicate.getExpressions().add(cb.equal(root.get("id"), map.get("id")));
                }
                //抖音
                if (map.get("dyID") != null && !"".equals(map.get("dyID"))) {
                    predicate.getExpressions().add(cb.equal(root.get("dyID"), map.get("dyID")));
                }
                //头条
                if (map.get("ttID") != null && !"".equals(map.get("ttID"))) {
                    predicate.getExpressions().add(cb.equal(root.get("ttID"), map.get("ttID")));
                }
                //火山
                if (map.get("hsID") != null && !"".equals(map.get("hsID"))) {
                    predicate.getExpressions().add(cb.equal(root.get("hsID"), map.get("hsID")));
                }
                //省
                if (map.get("provincesId") != null && !"".equals(map.get("provincesId"))) {
                    predicate.getExpressions().add(cb.equal(root.get("provincesId"), map.get("provincesId")));
                }
               //市
                if (map.get("citiesId") != null && !"".equals(map.get("citiesId"))) {
                    predicate.getExpressions().add(cb.equal(root.get("citiesId"), map.get("citiesId")));
                }
                if (map.get("userType") != null && !"".equals(map.get("userType"))) {
                    predicate.getExpressions().add(cb.like(root.get("userType").as(String.class), "%|"+(String)map.get("userType")+"|%"));
                }

                if (map.get("status") != null && !"".equals(map.get("status"))) {
                    predicate.getExpressions().add(cb.equal(root.get("status"), map.get("userType")));
                }



                return predicate;
            }
        };

    }

    public BaseUserKol save(BaseUserKol baseUserKol) {
        return kolDto.save(baseUserKol);
    }

    public void update(BaseUserKol baseUserKol) {
             kolDto.save(baseUserKol);
    }

    public BaseUserKol findByTelephone(String telephone) {
        return kolDto.findByTelephone(telephone);
    }

    public BaseUserKol findByEmail(String email) {
        return kolDto.findByEmail(email);

    }
}
